package org.ko.core.web.auth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ko.core.domain.auth.User;
import org.ko.core.logic.auth.UserSvc;
import org.ko.core.web.BaseController;
import org.ko.core.web.Result;
import org.ko.core.websupport.log.Log;
import org.ko.core.websupport.log.impl.LogUitls;
import org.ko.core.websupport.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/auth/user")
public class UserAct extends BaseController{
	
	@Autowired
	private UserSvc userSvc;
	
	@Override
	protected String getTemplatePath() {
		return "core/auth/user/";
	}

	@RequestMapping(value="add", method = RequestMethod.GET)
	public String add(){
		return view("add");
	}
	
	@RequiresPermissions("user:add")
	@Log(message="新增用户,用户名={0}",params={"name"},category=Log.USER)
	@RequestMapping(value="add", method = RequestMethod.POST)
	public String addDo(HttpServletRequest request, HttpServletResponse response, User bean){
		try {
			userSvc.addDo(bean);
			return "redirect:list.do";
		} catch (Exception e) {
			LogUitls.error(e);
			return redirect("add.do");
		}
	}
	
	@RequestMapping(value="edit",method = RequestMethod.GET)
	public String edit(
			HttpServletRequest request,
			HttpServletResponse response,
			ModelMap model,
			@RequestParam(value = "id", required = true)Long id){
		
		model.addAttribute("entity",userSvc.get(id));
		return view("edit");
	}
	
	/**
	 * 提交修改内容,完成后重定向到列表
	 * @param request
	 * @param response
	 * @param model
	 * @param bean
	 * @return
	 */
	@RequiresPermissions("user:edit")
	@Log(message="编辑用户,id={0}",params={"id"},category=Log.USER)
	@RequestMapping(value="edit",method = RequestMethod.POST)
	public String editDo(HttpServletRequest request,HttpServletResponse response,ModelMap model,User bean){
		
		try{
			userSvc.editDo(bean);
			return "redirect:list.do";
		}catch(Exception e){
			LogUitls.error(e);
			return redirect("edit.do?id="+bean.getId());
		}
	}
	
	/**
	 * 批量删除指定ID记录
	 * @param request
	 * @param response
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("user:delete")
	@Log(message="删除用户,id={0}",params={"id"},category=Log.USER)
	@RequestMapping(value="delete",method = RequestMethod.POST)
	public @ResponseBody Result delete(
			HttpServletRequest request,
			HttpServletResponse response,
			ModelMap model,
			@RequestParam(value = "ids", required = true)Long ids[]){
		Result result = new Result();
		try {
			userSvc.delete(ids);
			result.setMessage("操作成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
			
			LogUitls.error(e);
		}
		
		return result;
	}
	
	@RequestMapping(value="list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, ModelMap model, Page page,String searchText){
		model.addAttribute(PAGINATION, userSvc.findPage(page,searchText));
		
		return view(LIST);
	}
	
	/**
	 * 选择联系人
	 * 参数：
	 * function 	当窗口关闭时，父窗口的回调函数
	 * keyword		联系人关键字（姓名或者电话）
	 * ids			已经选择好的员工
	 * 
	 * @param request
	 * @param model
	 * @param page
	 * @return
	 */
	@RequestMapping(value="select")
	public String select(HttpServletRequest request, ModelMap model, Page page, String function, String keyword, String ids){
		if(isGET(request)){
			model.addAttribute("func", function);
			model.addAttribute("ids", ids);
			
			return view("select");
		}else{
			model.addAttribute(PAGINATION, userSvc.findForSelect(page, keyword));
			
			return view("select_data");
		}
	}
}